import { resources, Prefab } from "cc";
export const loadPrefab = (path: string): Promise<Prefab> => {
  return new Promise((resolve, reject) => {
    resources.load(path, Prefab, (err, data) => {
      if (err) reject(null);
      resolve(data);
    });
  });
};

export const debounce = ({ delay: l }, o) => {
  let e,
    d = !0;
  const t = (...i) => {
    d
      ? (clearTimeout(e),
        (e = setTimeout(() => {
          d && o(...i), (e = void 0);
        }, l)))
      : o(...i);
  };
  return (
    (t.isPending = () => e !== void 0),
    (t.cancel = () => {
      d = !1;
    }),
    (t.flush = (...i) => o(...i)),
    t
  );
};

export const sleep = (second: number) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, second * 1000);
  });
};
